import { createStore } from 'vuex'
import { getArticles, getArticle_Count,Article_Show } from '@/request/api/Articles.js'
import { getTest } from "@/request/api/backstage/index.js"

export default createStore({
  state: {
    pages: 0,
    page: 1,
    pagesize: 3,
    articlesList: [
      { titile: "中二病时刻", dateTime: "7-30", body: "这是为了未来得一切" ,imgUrl:"img/项目中心_我emo了_1660388750138.png"},
      { titile: "中二病时刻", dateTime: "7-30", body: "这是为了未来得一切",imgUrl:"img/项目中心_我emo了_1660388750138.png" },
      { titile: "中二病时刻", dateTime: "7-30", body: "这是为了未来得一切" ,imgUrl:"img/项目中心_我emo了_1660388750138.png"},
      { titile: "中二病时刻", dateTime: "7-30", body: "这是为了未来得一切" ,imgUrl:"img/项目中心_我emo了_1660388750138.png"},
    ],
    art_show:[{ titile: "中二病时刻", dateTime: "7-30", body: "这是为了未来得一切" ,imgUrl:"img/项目中心_我emo了_1660388750138.png"}],
    imgChoose: false,
    img: "",
    imgChoose2: false,
    img2s: {}
  },
  getters: {
  },
  mutations: {
    change_Articles(state, value) {
      state.articlesList = JSON.parse(JSON.stringify(value));
      state.articlesList.forEach(item => {
        let arr = [];
        let year = item.dateTime.slice(0, 4);
        let month = item.dateTime.slice(5, 7);
        let day = item.dateTime.slice(8, 10);
        arr.push(year, month, day)
        item["new_date"] = arr;
      })
    },
    change_art_show(state,value){
      state.art_show = value;
    },
    change_pages(state, value) {
      state.pages = Math.ceil(value / state.pagesize);
    },
    update_page(state, value) {
      state.page = value;
    },
    forwardBackward_page(state, value) {
      state.page += value;
    }
  },
  actions: {
    async get_Articles({ commit }, value) {
      let { data } = await getArticles(value);
      commit("change_Articles", data.msg);

    },
    async getArticle_Count({ commit }) {
      let { data } = await getArticle_Count();
      commit("change_pages", data.msg[0]["count(*)"]);
    },
    async get_test({ commit }, value) {
      let { data } = await getTest(value);
      console.log(data);
    },
    async get_Article_Show({commit},value){
      console.log(value);
      let {data} = await Article_Show(value);
      console.log(data);
      commit("change_art_show",data.msg);
    }
  },
  modules: {
  }
})
